package com.kitty.record.kittyio.bio.three;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Description
 * @Author SHEN
 * @Date 2021/2/8
 */
public class Server {

    public static void main(String[] args) {

        SocketThreadPool socketThreadPool = new SocketThreadPool();

        try {
            ServerSocket serverSocket = new ServerSocket(9999);

            while(true){
                Socket accept = serverSocket.accept();
                socketThreadPool.execute(()->{
                    InputStream inputStream = null;
                    BufferedReader bf = null;
                    try {
                        inputStream = accept.getInputStream();
                        bf = new BufferedReader(new InputStreamReader(inputStream));
                        String msg = null;
                        while ((msg = bf.readLine()) != null){
                            System.out.println(msg);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }finally {
                        if(inputStream != null){
                            try {
                                inputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        if(bf != null){
                            try {
                                bf.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
